/*
 * CreateOrUpdate.java
 *
 * Created on 14 marzec 2007, 10:51
 *
 */

package arocms.gallery;

import arocms.business.Image;
import arocms.business.ImageDAO;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.hibernate.Hibernate;

/**
 *
 * @author arek
 */
public class CreateOrUpdate extends Action{
    
    private String SUCCESS = "success";
    private String FAILURE = "failure";
    
    private Logger log = Logger.getLogger(this.getClass());
    /** Creates a new instance of CreateOrUpdate */
    public CreateOrUpdate() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.debug("--> execute");
        ImageForm imgForm = (ImageForm) form;
        Image img = new Image();
        Long pk = null;
        try {
            pk = new Long(imgForm.getId());
        } catch (Exception ex) {
            log.debug("id is NaN");
        }
        img.setId(pk);
        img.setTitle(imgForm.getTitle());
        img.setDescription(imgForm.getDescription());
        img.setDate(new Date());
        FormFile file = imgForm.getFile();
        img.setContentType(file.getContentType());
        img.setFileName(file.getFileName());
        img.setFileSize(Long.valueOf(file.getFileSize()));
        img.setContentData(Hibernate.createBlob(file.getFileData()));
        Image ret = new ImageDAO().createOrUpdate(img);
        log.debug("<-- execute, pk=" + pk + ", title=" + img.getTitle());
        if (ret.getId()==null || ret.getId().longValue()==0) {
            return mapping.findForward(FAILURE);
        } else {
            return mapping.findForward(SUCCESS);
        }
    }
}
